define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/report/models'),
        utils = require('js/utils/tools'),
        chart = require('js/utils/chart'),
        moment = require('moment'),
        helper = require('js/apps/report/common');

    var Top100View = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
            'click #exportData': 'exportData',
        },
        initialize: function (options) {
            this.options = options;
            this.model = new models.Top100List();
            this.model.bind('change reset', this.renderWithData, this);
            this.channels = new models.Channels();
            this.channels.bind('change reset', this.renderWithData, this);
        },
        exportData: function (e) {
            e.preventDefault();
            var d = {
                top_type: this.options.type,
                date_range: this.options.dateRange.join(',')
            }
            notify.warning('警告', '数据量较大，请耐心等待');
            utils.exportData(this.model.url, d);
            return false;
        },
        doSearch: function () {
            this.options.dateRange = helper.getDateRange();
            // console.log('getDateRange2',helper.getDateRange2())
            this.options.createRange = helper.getDateRange2();
            // this.options.type = $('#searchType').val();
            this.options.chn = utils.getInput('#chnFilter') || [];
            this.load();
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.model.list,
                options: this.options,
                channels: this.channels.list || [],
            }));
            helper.loadDatePicker(this.options);
            utils.multiselect('#chnFilter', true, {
                'includeSelectAllOption': true
            });
            var that = this;
            utils.renderTable('main-list', {
                $orderby: this.options.$orderby || 'rank',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    var desc = false;
                    if (field[0] == '-') {
                        desc = true;
                        field = field.substr(1);
                    }
                    that.model.list = _.sortBy(that.model.list, function (o) {
                        if (desc) return 0 - o[field];
                        return o[field];
                    });
                    that.renderWithData();
                }
            });
            return this;
        },
        load: function () {
            // this.model.fetch({
            //     reset: true,
            //     data: {
            //         top_type: this.options.type,
            //         date_range: this.options.dateRange.join(','),
            //     },
            //     error: function (c, r, o) {
            //         notify.notifyResp(r);
            //     }
            // })
            var data = {
                date_range: this.options.dateRange.join(','),
                create_range: this.options.createRange.join(','),
            };
            if (this.options.chn && this.options.chn.length > 0) {
                var chn = this.options.chn;
                if (chn.length == 1) {
                    data.chn = chn[0];
                } else {
                    data.chn = JSON.stringify({ '$in': this.options.chn });
                }
            }
            this.channels.fetch({
                reset: true,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            })
            this.model.fetch({
                reset: true,
                data: data,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "report/Top100.html",
        render: function () {
            var today = moment().format('YYYY-MM-DD'),
                dateRange = [today, today],
                createRange = ["2017-01-01", today],
                view = new Top100View({
                    type: "recharge",
                    dateRange: dateRange,
                    createRange: createRange
                });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });

});
